﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using SmartProject.Data;
using System.Data;
using System.Web.UI.WebControls;

namespace SmartProject.BusinessObjects
{
    public class Message : Entity
    {
        private int _workMessageId;
        private MessageDateType _messageDataType;
        private UserMessage _userMessage;

        public Message()
        {
            _messageDataType = new MessageDateType();
            _workMessageId = _messageDataType.GetId("WorkMessage", false);

            _userMessage = new UserMessage();

            DataParameter[] dataParameters = new DataParameter[7];
            dataParameters[0] = new DataParameter("MessageId", DataType.Integer);
            dataParameters[1] = new DataParameter("OwnerId", DataType.Integer);
            dataParameters[2] = new DataParameter("ReferenceId", DataType.Integer);
            dataParameters[3] = new DataParameter("ReferenceObjectId", DataType.Integer);
            dataParameters[4] = new DataParameter("Description", 7000);
            dataParameters[5] = new DataParameter("Created", DataType.DateTime);
            dataParameters[6] = new DataParameter("OwnerName", 350);
            base.Initialize(dataParameters);
            base.BusinessRules.Add(new BusinessRules.RemoveMessageAttachments());
        }

        public DataTable LoadWorkMessages()
        {
            SetAttribute("ReferenceId", _workMessageId);
            return base.Find("smartProject_GetMessages");
        }

        public List<int> LoadWorkMessageIdList()
        {
            List<int> messageIdList = new List<int>();
            SetAttribute("ReferenceId", _workMessageId);
            DataTable dt = base.Find("smartProject_GetReferenceMessageIdList");
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    messageIdList.Add(Convert.ToInt32(dt.Rows[i][0]));
                }
            }
            return messageIdList;
        }

        public override void Load()
        {
            throw new NotImplementedException();
        }

        public void SaveWorkMessage(int[] userIdList)
        {
            Save();
            int messageId = base.ReturnId;
            foreach (int userId in userIdList)
            {
                _userMessage.SetAttribute("MessageId", messageId);
                _userMessage.SetAttribute("UserId", userId);
                _userMessage.Save();
            }
        }

        public override void Save()
        {
            SetAttribute("ReferenceId", _workMessageId);
            base.Run("smartProject_AddMessage");
        }

        public override void Update()
        {
            throw new NotImplementedException();
        }

        public override void Remove()
        {
            base.Run("smartProject_RemoveMessage");
        }

        public void RemoveWorkMessages()
        {
            SetAttribute("ReferenceId", _workMessageId);
            base.Run("smartProject_RemoveReferenceMessage");
        }
    }
}
